<?php

namespace app\shop\controller\Order;

use think\Controller;

class Cart extends Controller
{
    // 购物车模型
    protected $CartModel = null;
    protected $ProductModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->CartModel = model('Order.Cart');
        $this->ProductModel = model('Product.Product');
    }

    public function index()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $business = model('Business.Business')->find($busid);

        if (!$business) {
            $this->error('用户不存在');
        }

        $CartData = $this->CartModel->with(['product'])->where(['busid' => $busid])->select();

        $this->success('查询成功', null, $CartData);
    }

    public function add()
    {
        $proid = $this->request->param('proid', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');

        $product = model('Product.Product')->find($proid);

        if (!$product) {
            $this->error('商品不存在');
        }

        $business = model('Business.Business')->find($busid);

        if (!$business) {
            $this->error('请先登录');
        }

        $cart = $this->CartModel->where(['busid' => $busid, 'proid' => $proid])->find();

        if ($cart) {
            // 进入里面说明购物车里面已经存在这个商品，更新

            // bcadd => 加
            $pronum = bcadd($cart['pronum'], 1);

            $data = [
                'pronum' => $pronum,
                // bcmul => 乘
                'total' => bcmul($cart['price'], $pronum, 2),
                'id' => $cart['id']
            ];

            $result = $this->CartModel->isUpdate(true)->save($data);
        } else {
            // 进入里面说明购物车不存在这个商品，新增

            // 组装数据
            $data = [
                'busid' => $busid,
                'proid' => $proid,
                'pronum' => 1,
                'price' => $product['price'],
                'total' => $product['price'],
            ];

            $result = $this->CartModel->validate('common/Order/Cart')->save($data);
        }

        if ($result === FALSE) {
            $this->error($this->CartModel->getError());
        } else {
            $this->success('添加到购物车成功');
        }
    }

    public function num()
    {
        $cartid = $this->request->param('cartid', 0, 'trim');
        $pronum = $this->request->param('pronum', 0, 'trim');

        $cart = $this->CartModel->find($cartid);

        if (!$cart) {
            $this->error('该数据不存在');
        }

        // 查询库存是否够
        $stock = model('Product.Product')->where('id', $cart['proid'])->value('stock');

        /* 
            库存：10 购物车：2 + 1 = 3 => 10 < 3
        */
        if ($stock < $pronum) {
            $this->error('库存不足');
        }

        $data = [
            'id' => $cartid,
            'pronum' => $pronum,
            'total' => bcmul($cart['price'], $pronum, 2)
        ];

        $result = $this->CartModel->isUpdate(true)->save($data);

        if ($result === FALSE) {
            $this->error('更新数量失败');
        } else {
            $this->success('更新数量成功');
        }
    }

    public function del()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $cartid = $this->request->param('cartid', 0, 'trim');

        $business = model('Business.Business')->find($busid);

        if (!$business) {
            $this->error('用户不存在');
        }

        $where = [
            'id' => $cartid,
            'busid' => $busid,
        ];

        $cart = $this->CartModel->where($where)->find();

        if (!$cart) {
            $this->error('该记录不存在');
        }

        $result = $cart->delete();

        if ($result === FALSE) {
            $this->error('删除记录不存在');
        } else {
            $this->success('删除记录成功');
        }
    }

    public function confirm()
    {
        // 如果前台从购物车页面进入，就获取到传入的购物车列表id
        $cartids = $this->request->param('cartids', '', 'trim');

        // 如果前台从商品详情页面进入，就获取到传入的商品id
        $proid = $this->request->param('proid', '', 'trim');
        // halt($cartids);

        if ($cartids) {
            $CartList = $this->CartModel->with(['product'])->whereIn('cart.id', $cartids)->select();
        }

        if ($proid) {
            $CartList = $this->ProductModel->where('id', $proid)->select();
        }


        if (!$CartList) {
            $this->error('选择的商品不存在');
        }

        $this->success('查询成功', null, $CartList);
    }
}
